package com.aatattoo.light_food_service.controller;

import com.aatattoo.light_food_service.entity.*;
import com.aatattoo.light_food_service.enumeration.ConstantType;
import com.aatattoo.light_food_service.service.*;
import com.aatattoo.light_food_service.utils.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.*;


@Controller
@ResponseBody
@RequestMapping("/dynamic")
public class DynamicController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private DynamicTagService dynamicTagService;
    @Autowired
    private MappingTagService mappingTagService;
    @Autowired
    private DynamicImgService dynamicImgService;
    @Autowired
    private RemindService remindService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserFollowService userFollowService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private LikeService likeService;
    @Autowired
    private FoodService foodService;

    /**
     * 获取指定标签相关动态
     * @param tagId
     * @return
     */
    @GetMapping("/forTag/{tagId}/{pageNum}")
    public Map<String, Object> getDynamicForTag(@PathVariable("tagId")int tagId,@PathVariable("pageNum")int pageNum,@RequestHeader("token")String token){
        //检查所有符合标签ID的动态，在判断次动态是否公开
        List<Integer> relevantDynamic = mappingTagService.getRelevantDynamicId(tagId);
        int myId = tokenService.analysisUserId(token);
        // 设置分页查询参数
        //pageSize:一页记录的数据量
        PageHelper.startPage(pageNum,10);
        List<Dynamic> dynamicList=new ArrayList<>();
        if (!relevantDynamic.isEmpty()){
            dynamicList=dynamicService.getDynamicForTag(relevantDynamic);
        }
        return pagingQuery(dynamicList,myId,myId,1);
    }

    //获取指定用户下的动态
    @GetMapping("/othRecommend/{userId}/{pageNum}")
    public  Map<String, Object> getOthDynamic(@PathVariable("userId") int userId,@PathVariable("pageNum")int pageNum,@RequestHeader("token")String token){
        int myId = tokenService.analysisUserId(token);
        // 设置分页查询参数
        //pageSize:一页记录的数据量
        PageHelper.startPage(pageNum,10);
        List<Dynamic> dynamicList=dynamicService.getDynamicForPublicByOthUser(userId);
        return pagingQuery(dynamicList,myId,userId,3);
    }

    /**
     * 分页查询动态
     * @param type 0：首页推荐 1：朋友圈
     * @param pageNum  页数
     * @return
     */
    @GetMapping("/recommend/{type}/{pageNum}")
    public  Map<String, Object> getDynamic(@PathVariable("type") int type,@PathVariable("pageNum") int pageNum,@RequestHeader("token")String token){
        // 设置分页查询参数
        //pageSize:一页记录的数据量
        PageHelper.startPage(pageNum,10);
        List<Dynamic> dynamicList = new ArrayList<>();
        int userId = tokenService.analysisUserId(token);
        switch (type){
            //DYNAMIC_RECOMMEND 获取首页推荐动态
            case 0:
                dynamicList= dynamicService.getDynamicForPublic();
                break;
            //DYNAMIC_FRIENDS 获取朋友圈动态
            case 1:
                List<UserFollow> userFollows = userFollowService.followFindById(userId);
                List<Integer> idList=new ArrayList<>();
                userFollows.forEach(userFollow -> {
                    idList.add(userFollow.getFollowId());
                });
                idList.add(userId);
                dynamicList = dynamicService.getDynamicForPublicByUserId(idList);
                break;
            //DYNAMI_MINE 获取我的动态
            case 2:
                dynamicList = dynamicService.getDynamicForMine(userId);
                break;
            default:
                break;
        }
        return pagingQuery(dynamicList,userId,userId,type);
    }

    @PostMapping("/delete")
    public int deleteDynamic(@RequestBody Dynamic id){
        Dynamic byId = dynamicService.findById(id.getId());
        if (byId.getHaveImg()==ConstantType.DYNAMIC_IMG.getType()){
            //删除旧关联的表内容与文件
            dynamicImgService.deleteImgAllFile(byId.getId());
            dynamicImgService.deleteDynamicImg(byId.getId());
        }
        if (byId.getHaveTag()==ConstantType.DYNAMIC_TAG.getType()){
            //删除旧关联表单
            mappingTagService.deleteByDynamicId(byId.getId());
        }
        if (byId.getHaveRemind()==ConstantType.DYNAMIC_REMIND.getType()){
            //删除旧关联表单
            Remind remind=new Remind();
            remind.setType(ConstantType.REMIND_DYNAMIC.getType());
            remind.setParentID(byId.getId());
            remindService.deleteByParentId(remind);
        }
        //删除关联的评论与回复
        List<Comment> commentList = commentService.findByDynamicId(id.getId());
        commentList.forEach(comment -> {
            //删除回复
            replyService.deleteByComent(comment.getId());
        });
        //删除评论
        commentService.deleteByDynamicId(id.getId());
        return dynamicService.deleteById(id.getId());
    }

    @PostMapping("type")
    public int updataType(@RequestBody Dynamic dynamic){
        return dynamicService.updataType(dynamic);
    }

    @PostMapping("/compile")
    public int updataDynamic(@RequestBody DynamicRelease dynamicRelease,@RequestHeader("token")String token){
        int userId = tokenService.analysisUserId(token);
        //1、获取跟新前表单对比新表单有哪些变动
        Dynamic byId = dynamicService.findById(dynamicRelease.getId());
        Dynamic dynamic=new Dynamic();
        int imgSize = dynamicRelease.getImg().size();
        int tagSize = dynamicRelease.getTag().size();
        int remindSize = dynamicRelease.getRemind().size();
        dynamic.setId(byId.getId());
        dynamic.setUserId(userId);
        dynamic.setContent(dynamicRelease.getContent());
        dynamic.setHaveImg(imgSize==0 ? ConstantType.DYNAMIC_UNIMG.getType():ConstantType.DYNAMIC_IMG.getType());
        dynamic.setHaveTag(tagSize==0 ? ConstantType.DYNAMIC_UNTAG.getType():ConstantType.DYNAMIC_TAG.getType());
        dynamic.setHaveRemind(remindSize==0 ? ConstantType.DYNAMIC_UNREMIND.getType():ConstantType.DYNAMIC_REMIND.getType());
        dynamic.setType(dynamicRelease.getType());
        dynamic.setForward(dynamicRelease.getForward());
        //旧没有数据，新有，处理关联表后更新
        //旧没有数据，新也没有，直接更新
        //旧有数据，新没有，直接更新，并删除老版本关联内容
        //旧有数据，新也有，比对变化，跟新新内容
        /**
         * 图片更新逻辑
         */
        if (byId.getHaveImg() == ConstantType.DYNAMIC_UNIMG.getType()) {
            if (dynamic.getHaveImg()==ConstantType.DYNAMIC_IMG.getType()){
                dynamicRelease.getImg().forEach(img->{
                    //更新dynamic_img表单
                    DynamicImg dynamicImg=new DynamicImg();
                    dynamicImg.setId(img);
                    dynamicImg.setDynamicID(byId.getId());
                    dynamicImgService.updataDynamicImg(dynamicImg);
                });
            }
        }else {
            if (dynamic.getHaveImg()== ConstantType.DYNAMIC_UNIMG.getType()){
                //删除旧关联的表内容与文件
                dynamicImgService.deleteImgAllFile(byId.getId());
                dynamicImgService.deleteDynamicImg(byId.getId());
            }else {
                //旧数据更新数据比对，不存在，删除旧数据文件与关联表，再用新数据与就数据比对，存在，跟新排序，不存在，更新内容关联表
                List<Object> imgId = dynamicImgService.getID(byId.getId());
                //旧数据比对新数据
                imgId.forEach(img->{
                    if (!dynamicRelease.getImg().contains(img)){
                        //删除更新图片数据中被删除的图片数据与关联内容
                        dynamicImgService.deleteImgFile((Integer) img);
                        dynamicImgService.deleteById((Integer) img);
                    }
                });
                //新数据比对旧数据
                dynamicRelease.getImg().forEach(img->{
                    DynamicImg dynamicImg=new DynamicImg();
                    dynamicImg.setId(img);
                    dynamicImg.setOrder(dynamicRelease.getImg().indexOf(img)+1);
                    //图片是否有未修改内容，有更新排序，没有更新新图片索引和排序
                    if (imgId.contains(img)){
                        dynamicImgService.updataDynamicOrder(dynamicImg);
                    }else{
                        dynamicImg.setDynamicID(byId.getId());
                        dynamicImgService.updataDynamicImgOrder(dynamicImg);
                    }
                });
            }
        }
        /**
         * 标签更新逻辑
         */
        if (byId.getHaveTag()==ConstantType.DYNAMIC_UNTAG.getType()){
            if (dynamic.getHaveTag()==ConstantType.DYNAMIC_TAG.getType()){
                dynamicRelease.getTag().forEach(tag->{
                    if (dynamicTagService.isTableExist(tag)==0){
                        //没有当前tag，插入值tag表，返回id，为映射表插入动态id和标签id
                        DynamicTag dynamicTag=new DynamicTag();
                        dynamicTag.setContent(tag);
                        dynamicTag.setType(ConstantType.TAG_USER.getType());
                        int tagId = dynamicTagService.addDynamicTagUser(dynamicTag);
                        MappingTag mappingTag=new MappingTag();
                        mappingTag.setDynamicId(byId.getId());
                        mappingTag.setTagId(tagId);
                        mappingTag.setOrder(dynamicRelease.getTag().indexOf(tag)+1);
                        mappingTagService.addMappingTag(mappingTag);
                    }else {
                        //为映射表插入动态id和标签id
                        DynamicTag dynamicTag = dynamicTagService.findByContent(tag);
                        MappingTag mappingTag=new MappingTag();
                        mappingTag.setDynamicId(byId.getId());
                        mappingTag.setTagId(dynamicTag.getId());
                        mappingTag.setOrder(dynamicRelease.getTag().indexOf(tag)+1);
                        mappingTagService.addMappingTag(mappingTag);
                    }
                });
            }
        }else {
            if (dynamic.getHaveTag()==ConstantType.DYNAMIC_UNTAG.getType()){
                //删除旧关联表单
                mappingTagService.deleteByDynamicId(byId.getId());
            }else {
                List<MappingTag> oldMapping = mappingTagService.findAllByDynamicId(byId.getId());
                List<String> oldContent=new ArrayList<>();
                //旧数据比对新数据
                oldMapping.forEach(mappingTag->{
                    DynamicTag dynamicTag = dynamicTagService.findById(mappingTag.getTagId());
                    //存在，取出老标签正文信息。不存在，删除旧映射表关系内容
                    if (dynamicRelease.getTag().contains(dynamicTag.getContent())){
                        oldContent.add(dynamicTag.getContent());
                    }else {
                        mappingTagService.deleteMappingById(mappingTag.getId());
                    }
                });
                //新数据比对旧数据
                dynamicRelease.getTag().forEach(content->{
                    if (oldContent.contains(content)){
                        MappingTag mappingTag=new MappingTag();
                        mappingTag.setDynamicId(byId.getId());
                        //获取标签id
                        DynamicTag byContent = dynamicTagService.findByContent(content);
                        mappingTag.setTagId(byContent.getId());
                        //获取映射对应id
                        int idByTag = mappingTagService.getIdByTag(mappingTag);
                        //更新映射表单排序(order)
                        mappingTag.setId(idByTag);
                        mappingTag.setOrder(dynamicRelease.getTag().indexOf(content)+1);
                        mappingTagService.updataMappingOrder(mappingTag);
                    }else {
                        if (dynamicTagService.isTableExist(content)==0){
                            //没有当前tag，插入值tag表，返回id，为映射表插入动态id和标签id
                            DynamicTag dynamicTag=new DynamicTag();
                            dynamicTag.setContent(content);
                            dynamicTag.setType(ConstantType.TAG_USER.getType());
                            int tagId = dynamicTagService.addDynamicTagUser(dynamicTag);
                            MappingTag mappingTag=new MappingTag();
                            mappingTag.setDynamicId(byId.getId());
                            mappingTag.setTagId(tagId);
                            mappingTag.setOrder(dynamicRelease.getTag().indexOf(content)+1);
                            mappingTagService.addMappingTag(mappingTag);
                        }
                        else {
                            //为映射表插入动态id和标签id和排序
                            DynamicTag dynamicTag = dynamicTagService.findByContent(content);
                            MappingTag mappingTag=new MappingTag();
                            mappingTag.setDynamicId(byId.getId());
                            mappingTag.setTagId(dynamicTag.getId());
                            mappingTag.setOrder(dynamicRelease.getTag().indexOf(content)+1);
                            mappingTagService.addMappingTag(mappingTag);
                        }
                    }
                });
            }

        }
        /**
         * @功能更新逻辑
         */
        if (byId.getHaveRemind()==ConstantType.DYNAMIC_UNREMIND.getType()){
            if (dynamic.getHaveRemind()==ConstantType.DYNAMIC_REMIND.getType() ){
                dynamicRelease.getRemind().forEach(passiveID->{
                    Remind remind=new Remind();
                    remind.setActiveId(userId);
                    remind.setPassiveId(passiveID);
                    remind.setType(ConstantType.REMIND_DYNAMIC.getType());
                    remind.setParentID(byId.getId());
                    remind.setOrder(dynamicRelease.getRemind().indexOf(passiveID)+1);
                    remindService.addRemind(remind);
                });
            }
        } else {
            if (dynamic.getHaveRemind()==ConstantType.DYNAMIC_UNREMIND.getType()){
                //删除旧关联表单
                Remind remind=new Remind();
                remind.setType(ConstantType.REMIND_DYNAMIC.getType());
                remind.setParentID(byId.getId());
                remindService.deleteByParentId(remind);
            }else {
                //获取旧关联信息用户的全部id，比对新关联用户id，旧比新：不存在数据的关联删除，新比旧，不存在的数据新增关联
                Remind remind=new Remind();
                remind.setActiveId(userId);
                remind.setType(ConstantType.REMIND_DYNAMIC.getType());
                remind.setParentID(byId.getId());
                List<Remind> allUPassiveId = remindService.getAllByParentId(remind);
                List<Integer> oldPassiveId=new ArrayList<>();
                //旧数据比对新数据
                allUPassiveId.forEach(oldRemind->{
                    if (dynamicRelease.getRemind().contains(oldRemind.getPassiveId())){
                        oldPassiveId.add(oldRemind.getPassiveId());
                    }else {
                        remindService.deleteById(oldRemind.getId());
                    }
                });
                //新数据比对旧数据
                dynamicRelease.getRemind().forEach(passiveId->{
                    if(oldPassiveId.contains(passiveId)){
                        //获取id
                        remind.setPassiveId(passiveId);
                        int id = remindService.getId(remind);
                        //更新排序
                        remind.setId(id);
                        remind.setOrder(dynamicRelease.getRemind().indexOf(passiveId)+1);
                        remindService.updataOrder(remind);
                    }else {
                        remind.setPassiveId(passiveId);
                        remind.setOrder(dynamicRelease.getRemind().indexOf(passiveId)+1);
                        remindService.addRemind(remind);
                    }
                });
            }

        }
        return dynamicService.updataDynamic(dynamic);
    }

    @PostMapping("/release")
    public int addDynamic(@RequestBody DynamicRelease dynamicRelease, @RequestHeader("token")String token){
        //1、先获取动态信息，将动态表单插入表中，返回插入的动态id
        int userId = tokenService.analysisUserId(token);
        Dynamic dynamic=new Dynamic();
        int imgSize = dynamicRelease.getImg().size();
        int tagSize = dynamicRelease.getTag().size();
        int remindSize = dynamicRelease.getRemind().size();
        dynamic.setUserId(userId);
        dynamic.setContent(dynamicRelease.getContent());
        dynamic.setHaveImg(imgSize==0 ? ConstantType.DYNAMIC_UNIMG.getType():ConstantType.DYNAMIC_IMG.getType());
        dynamic.setHaveTag(tagSize==0 ? ConstantType.DYNAMIC_UNTAG.getType():ConstantType.DYNAMIC_TAG.getType());
        dynamic.setHaveRemind(remindSize==0 ? ConstantType.DYNAMIC_UNREMIND.getType():ConstantType.DYNAMIC_REMIND.getType());
        dynamic.setType(dynamicRelease.getType());
        dynamic.setForward(dynamicRelease.getForward());
        //动态Id
        int dynamicId = dynamicService.addDynamic(dynamic);
        if (imgSize!=0){
            dynamicRelease.getImg().forEach(img->{
                //更新dynamic_img表单
                DynamicImg dynamicImg=new DynamicImg();
                dynamicImg.setId(img);
                dynamicImg.setDynamicID(dynamicId);
                dynamicImgService.updataDynamicImg(dynamicImg);
            });
        }
        if (tagSize!=0){
            dynamicRelease.getTag().forEach(tag->{
                if (dynamicTagService.isTableExist(tag)==0){
                    //没有改tag，插入值tag表，返回id，为映射表插入动态id和标签id
                    DynamicTag dynamicTag=new DynamicTag();
                    dynamicTag.setContent(tag);
                    dynamicTag.setType(ConstantType.TAG_USER.getType());
                    int tagId = dynamicTagService.addDynamicTagUser(dynamicTag);
                    MappingTag mappingTag=new MappingTag();
                    mappingTag.setDynamicId(dynamicId);
                    mappingTag.setTagId(tagId);
                    mappingTag.setOrder(dynamicRelease.getTag().indexOf(tag)+1);
                    mappingTagService.addMappingTag(mappingTag);
                }else {
                    //为映射表插入动态id和标签id
                    DynamicTag dynamicTag = dynamicTagService.findByContent(tag);
                    MappingTag mappingTag=new MappingTag();
                    mappingTag.setDynamicId(dynamicId);
                    mappingTag.setTagId(dynamicTag.getId());
                    mappingTag.setOrder(dynamicRelease.getTag().indexOf(tag)+1);
                    mappingTagService.addMappingTag(mappingTag);
                }
            });
        }
        if (remindSize!=0){
            dynamicRelease.getRemind().forEach(passiveID->{
                Remind remind=new Remind();
                remind.setActiveId(userId);
                remind.setPassiveId(passiveID);
                remind.setType(ConstantType.REMIND_DYNAMIC.getType());
                remind.setParentID(dynamicId);
                remind.setOrder(dynamicRelease.getRemind().indexOf(passiveID)+1);
                remindService.addRemind(remind);
            });
        }

        return 1;
    }

    /**
     *
     * @param dynamicList
     * @param myId
     * @param type 0,1-每条动态都携带用户参数
     *             2-我的动态，只显示一天用户参数，我的
     *             3-只显示一条用户参数（详细）其他用户

     * @return
     */
    private  Map<String, Object> pagingQuery(List<Dynamic> dynamicList,int myId,int othUserId,int type){
        // 封装分页查询结果到 PageInfo 对象中以获取相关分页信息
        PageInfo pageInfo = new PageInfo( dynamicList );
//        System.out.println("总页数: " + pageInfo.getPages());
//        System.out.println("总记录数: " + pageInfo.getTotal());
//        System.out.println("当前页数: " + pageInfo.getPageNum());
//        System.out.println("当前页面记录数量: " + pageInfo.getSize());
        Map<String,Object> mapData=new LinkedHashMap<>();
        List<Object> mapList=new ArrayList<>();
        Map<String,Object> pageMap=new LinkedHashMap<>();
        pageMap.put("pageCount",pageInfo.getPages());//一共多少页
        pageMap.put("pageNumber",pageInfo.getPageNum());//现在正在多少页
        mapData.put("page",pageMap);
//       获取个人动态或指定用户动态，只需要提供一次用户信息
        Map<String,Object> userMap=new LinkedHashMap<>();
        if (type==3){
            if (!mapData.containsKey("user")){
                //获取发布动态的用户信息
                User dynamicUser = userService.fingById(othUserId);
                UserFollow userFollow=new UserFollow();
                userFollow.setFollowId(othUserId);
                userFollow.setFansId(myId);
                int follow = userFollowService.findFollowByBothId(userFollow);
                int mutual = userFollowService.mutualAttentionFindById(userFollow);
                int countFollow = userFollowService.countFollowById(othUserId);
                int countFans = userFollowService.countFansById(othUserId);
                int countDynamic = dynamicService.countDynamicByUserId(othUserId);
                userMap.put("id",dynamicUser.getId());
                userMap.put("name",dynamicUser.getName());
                userMap.put("gender",dynamicUser.getGender());
                userMap.put("avatar",dynamicUser.getAvatar());
                userMap.put("follow", follow != 0);
                userMap.put("mutualAttention",mutual!=0);
                userMap.put("countFollow",countFollow);
                userMap.put("countFans",countFans);
                userMap.put("dynamic",countDynamic);
                mapData.put("user",userMap);
            }
        }
        if (type==2){
            if (!mapData.containsKey("user")){
                //获取发布动态的用户信息
                mapData.put("user",userService.getUserMap(myId));
            }
        }
        pageInfo.getList().forEach(list -> {
            Dynamic dynamic=(Dynamic)list;
            List<Object> imgList=new ArrayList<>();
            List<Object> tagList=new ArrayList<>();
            List<Object> remindList=new ArrayList<>();
            Map<String,Object> forwadrMap=new HashMap<>();
            Map<String,Object> map=new LinkedHashMap<>();
            Map<String,Object> othUserMap=new LinkedHashMap<>();
            if (type==1||type==0){
                //            每条动态都需要携带用户参数
                //获取发布动态的用户信息
                User dynamicUser = userService.fingById(dynamic.getUserId());
                UserFollow userFollow=new UserFollow();
                userFollow.setFollowId(dynamic.getUserId());
                userFollow.setFansId(myId);
                int follow = userFollowService.findFollowByBothId(userFollow);
                int mutual = userFollowService.mutualAttentionFindById(userFollow);
                othUserMap.put("id",dynamicUser.getId());
                othUserMap.put("name",dynamicUser.getName());
                othUserMap.put("avatar",dynamicUser.getAvatar());
                othUserMap.put("follow", follow != 0);
                othUserMap.put("mutualAttention",mutual!=0);
                map.put("user",othUserMap);
            }
            map.put("id",dynamic.getId());
            map.put("content",dynamic.getContent());
            if (dynamic.getHaveImg()== ConstantType.DYNAMIC_IMG.getType()){
                List<DynamicImg> dynamicImgs = dynamicImgService.findAllByDynamicID(dynamic.getId());
                dynamicImgs.forEach(dynamicImg -> {
                    Map<String,Object> imgMap=new LinkedHashMap<>();
                    imgMap.put("id",dynamicImg.getId());
                    imgMap.put("address",dynamicImg.getAddress());
                    imgMap.put("order",dynamicImg.getOrder());
                    imgList.add(imgMap);
                });
            }
            if (dynamic.getHaveTag()==ConstantType.DYNAMIC_TAG.getType()){
                List<MappingTag> mappingTags = mappingTagService.findAllByDynamicId(dynamic.getId());
                mappingTags.forEach(mappingTag -> {
                    Map<String,Object> tagMap=new LinkedHashMap<>();
                    DynamicTag dynamicTag = dynamicTagService.findById(mappingTag.getTagId());
                    tagMap.put("id",dynamicTag.getId());
                    tagMap.put("content",dynamicTag.getContent());
                    tagMap.put("order",mappingTag.getOrder());
                    tagList.add(tagMap);
                });
            }
            if (dynamic.getHaveRemind()==ConstantType.DYNAMIC_REMIND.getType()){
                Remind forReminds=new Remind();
                forReminds.setType(ConstantType.REMIND_DYNAMIC.getType());
                forReminds.setParentID(dynamic.getId());
                List<Remind> reminds = remindService.getAllByParentId(forReminds);
                System.out.println(reminds);
                reminds.forEach(remind -> {
                    User user = userService.fingById(remind.getPassiveId());
                    System.out.println(user);
                    Map<String,Object> remindMap=new LinkedHashMap<>();
                    remindMap.put("id",remind.getPassiveId());
                    remindMap.put("userName",user.getName());
                    remindMap.put("order",remind.getOrder());
                    remindList.add(remindMap);
                });
            }
            if (dynamic.getForward() != 0){
                 int id = dynamic.getForward();
                 Recipes recipes = foodService.getRecipes(id);
                 forwadrMap.put("id",recipes.getId());
                 forwadrMap.put("name",recipes.getName());
                 forwadrMap.put("photo",recipes.getPhoto());
                 forwadrMap.put("more",recipes.getMore());
            }
            map.put("img",imgList);
            map.put("tag",tagList);
            map.put("remind",remindList);
            map.put("type",dynamic.getType());
            map.put("forward",forwadrMap);
            //动态喜欢、评论、收藏的数量
//            喜欢
            Like like=new Like();
            like.setType(1);
            like.setParentId(dynamic.getId());
            like.setUserId(myId);
            int dynamicLike = likeService.countLike(like);
//            评论
            int commentCount=0;
            commentCount = commentCount + commentService.countComment(dynamic.getId());
            List<Integer> idList = commentService.findByDynamicIdForId(dynamic.getId());
            commentCount += idList.stream().mapToInt(id -> replyService.countReply(id)).sum();
//            收藏
            Collect collect=new Collect();
            collect.setType(2);
            collect.setParentId(dynamic.getId());
            collect.setUserId(myId);
            int collectCount = foodService.countCollect(collect);
            map.put("like",dynamicLike);
            map.put("is_like",likeService.checkLike(like) == 1);
            map.put("comment",commentCount);
            map.put("collection",collectCount);
            map.put("is_collection",foodService.checkCollect(collect)==1);
            map.put("createTime", TimeUtil.timestampFormat(dynamic.getCreateTime()));
            mapList.add(map);
        });
        mapData.put("dynamic",mapList);
        return mapData;
    }
}
